<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 7.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16.ico">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" integrity="sha256-wiz7ZSCn/btzhjKDQBms9Hx4sSeUYsDrTLg7roPstac=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.css" integrity="sha256-6cQIC71/iBIYXFK+0RHAvwmjwWzkWd+r7v/BX3/vZDc=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/themes/green/pace-theme-minimal.css">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/pace.min.js" integrity="sha256-gqd7YTjg/BtfqWSwsJOvndl0Bxc8gFImLEkXQT8+qj0=" crossorigin="anonymous"></script>

<script class="next-config" data-name="main" type="application/json">{"hostname":"sumumm.github.io","root":"/","images":"/images","scheme":"Gemini","darkmode":false,"version":"8.19.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":true,"style":"mac"},"fold":{"enable":true,"height":300},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":true,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="本文主要是数据结构——队列相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:type" content="article">
<meta property="og:title" content="LV03-09-数据结构-队列">
<meta property="og:url" content="https://sumumm.github.io/post/ddef650e.html">
<meta property="og:site_name" content="苏木">
<meta property="og:description" content="本文主要是数据结构——队列相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV03-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/LV03-09-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84-%E9%98%9F%E5%88%97/img/image-20220501085957416.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV03-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/LV03-09-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84-%E9%98%9F%E5%88%97/img/image-20220501091028672.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV03-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/LV03-09-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84-%E9%98%9F%E5%88%97/img/1.gif">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV03-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/LV03-09-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84-%E9%98%9F%E5%88%97/img/2.gif">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV03-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/LV03-09-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84-%E9%98%9F%E5%88%97/img/3.gif">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV03-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/LV03-09-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84-%E9%98%9F%E5%88%97/img/image-20220501161801389.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV03-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/LV03-09-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84-%E9%98%9F%E5%88%97/img/image-20220501175330538.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV03-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/LV03-09-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84-%E9%98%9F%E5%88%97/img/4.gif">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV03-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/LV03-09-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84-%E9%98%9F%E5%88%97/img/5.gif">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV03-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/LV03-09-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84-%E9%98%9F%E5%88%97/img/6.gif">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV03-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/LV03-09-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84-%E9%98%9F%E5%88%97/img/7.gif">
<meta property="article:published_time" content="2022-04-25T01:44:08.000Z">
<meta property="article:modified_time" content="2025-06-13T16:25:56.991Z">
<meta property="article:author" content="苏木">
<meta property="article:tag" content="LV03-数据结构">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV03-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/LV03-09-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84-%E9%98%9F%E5%88%97/img/image-20220501085957416.png">


<link rel="canonical" href="https://sumumm.github.io/post/ddef650e.html">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://sumumm.github.io/post/ddef650e.html","path":"post/ddef650e.html","title":"LV03-09-数据结构-队列"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>LV03-09-数据结构-队列 | 苏木</title>
  








    <script src="/js/browser_tools_disable.js"></script>

  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
<!-- hexo injector head_end start --><link rel="stylesheet" href="https://unpkg.com/hexo-next-tags-plus@latest/lib/tag_plus.css" media="defer" onload="this.media='all'"><!-- hexo injector head_end end --></head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">苏木</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">我的学习之路</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>苏木的家</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类页<span class="badge">42</span></a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档页<span class="badge">673</span></a></li><li class="menu-item menu-item-flink"><a href="/flink/" rel="section"><i class="fa fa-link fa-fw"></i>友人帐</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于我</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%80%E3%80%81%E9%98%9F%E5%88%97%E7%9A%84%E6%A6%82%E5%BF%B5"><span class="nav-text">一、队列的概念</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%8C%E3%80%81%E9%A1%BA%E5%BA%8F%E9%98%9F%E5%88%97"><span class="nav-text">二、顺序队列</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E5%AD%98%E5%82%A8%E7%BB%93%E6%9E%84"><span class="nav-text">1.存储结构</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E7%BB%93%E6%9E%84%E4%BD%93%E5%AE%9E%E7%8E%B0"><span class="nav-text">2.结构体实现</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E9%A1%BA%E5%BA%8F%E9%98%9F%E5%88%97%E5%9F%BA%E6%9C%AC%E6%93%8D%E4%BD%9C"><span class="nav-text">3.顺序队列基本操作</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#3-1%E5%88%9B%E5%BB%BA"><span class="nav-text">3.1创建</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-2%E9%87%8A%E6%94%BE"><span class="nav-text">3.2释放</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-3%E6%98%BE%E7%A4%BA"><span class="nav-text">3.3显示</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-4%E5%85%A5%E9%98%9F"><span class="nav-text">3.4入队</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-5%E5%87%BA%E9%98%9F"><span class="nav-text">3.5出队</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-6%E7%A9%BA%E6%BB%A1"><span class="nav-text">3.6空满</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-7%E6%B8%85%E7%A9%BA"><span class="nav-text">3.7清空</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%89%E3%80%81%E9%93%BE%E5%BC%8F%E9%98%9F%E5%88%97"><span class="nav-text">三、链式队列</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E5%AD%98%E5%82%A8%E7%BB%93%E6%9E%84-1"><span class="nav-text">1.存储结构</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E7%BB%93%E6%9E%84%E4%BD%93%E5%AE%9E%E7%8E%B0-1"><span class="nav-text">2.结构体实现</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E9%93%BE%E5%BC%8F%E9%98%9F%E5%88%97%E5%9F%BA%E6%9C%AC%E6%93%8D%E4%BD%9C"><span class="nav-text">3.链式队列基本操作</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#3-1%E5%88%9B%E5%BB%BA-1"><span class="nav-text">3.1创建</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-2%E5%85%A5%E9%98%9F"><span class="nav-text">3.2入队</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-3%E5%87%BA%E9%98%9F"><span class="nav-text">3.3出队</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-4%E9%87%8A%E6%94%BE"><span class="nav-text">3.4释放</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-5%E6%B8%85%E7%A9%BA"><span class="nav-text">3.5清空</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-6%E6%98%BE%E7%A4%BA"><span class="nav-text">3.6显示</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-7%E5%88%A4%E7%A9%BA"><span class="nav-text">3.7判空</span></a></li></ol></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="苏木"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">苏木</p>
  <div class="site-description" itemprop="description">莫道桑榆晚，为霞尚满天</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">673</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">42</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">43</span>
        <span class="site-state-item-name">标签</span>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <a href="https://github.com/sumumm" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;sumumm" rel="noopener me" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
  </div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://sumumm.github.io/post/ddef650e.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="苏木">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="苏木">
      <meta itemprop="description" content="莫道桑榆晚，为霞尚满天">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="LV03-09-数据结构-队列 | 苏木">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          LV03-09-数据结构-队列
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2022-04-25 09:44:08" itemprop="dateCreated datePublished" datetime="2022-04-25T09:44:08+08:00">2022-04-25</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/" itemprop="url" rel="index"><span itemprop="name">嵌入式开发</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/" itemprop="url" rel="index"><span itemprop="name">01HQ课程体系</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV03-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/" itemprop="url" rel="index"><span itemprop="name">LV03-数据结构</span></a>
        </span>
    </span>

  
    <span class="post-meta-break"></span>
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>5.7k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>21 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><p>本文主要是数据结构——队列相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。</p>
<span id="more"></span>

<!-- Photo: https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV03-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/LV03-09-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84-%E9%98%9F%E5%88%97/img/ -->

<details class="folding-tag" blue><summary> 点击查看使用工具及版本 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center" width=150px>Windows</td>        <td align="left">windows11</td>    </tr>    <tr>        <td align="center">Ubuntu</td>        <td align="left">Ubuntu16.04的64位版本</td>      </tr>    <tr>        <td align="center">VMware® Workstation 16 Pro</td>        <td align="left">16.2.3 build-19376536</td>      </tr>    <tr>        <td align="center">SecureCRT</td>        <td align="left">Version 8.7.2 (x64 build 2214)   -   正式版-2020年5月14日</td>      </tr>    <tr>        <td align="center">开发板</td>        <td align="left">正点原子 i.MX6ULL Linux阿尔法开发板</td>      </tr>    <tr>        <td align="center">uboot</td>        <td align="left">NXP官方提供的uboot，NXP提供的版本为uboot-imx-rel_imx_4.1.15_2.1.0_ga(使用的uboot版本为U-Boot 2016.03)</td>      </tr>    <tr>        <td align="center">linux内核</td>        <td align="left">linux-4.15(NXP官方提供)</td>      </tr>    <tr>        <td align="center">STM32开发板</td>        <td align="left">正点原子战舰V3(STM32F103ZET6)</td>      </tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看本文参考资料 </summary>
              <div class='content'>
              <table>    <tr><td align="center">参考方向  </td><td align="center">参考原文</td></tr>    <tr><td align="left">---</td><td align="left"><a href="" target="_blank">--- <i class="fa fa-external-link-alt"></i> </a></td></tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看相关文件下载 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center">---</td>        <td align="left">--- <a href="" target="_blank">  <i class="fa fa-external-link-alt"></i></a></td>      </tr></table>
              </div>
            </details>

<h1 id="一、队列的概念"><a href="#一、队列的概念" class="headerlink" title="一、队列的概念"></a><font size=3>一、队列的概念</font></h1><p>队列是限制在两端进行插入操作和删除操作的<strong>线性表</strong>。</p>
<ul>
<li><strong>队尾</strong>：允许进行存入操作的一端称为队尾。</li>
<li><strong>队头</strong>：允许进行删除操作的一端称为队头。</li>
<li><strong>空队</strong>：当线性表中没有元素时，称为空队。</li>
<li><strong>入队</strong>：数据元素进队列的过程称为入队。</li>
<li><strong>出队</strong>：数据元素出队列的过程称为 出队。</li>
</ul>
<p>队列的特点是<strong>先进先出</strong>（ FIFO ），数据从队列的一端进，从另一端出，可以由顺序表实现队列的存储结构，称为顺序队列，也可以由链表实现队列的存储结构，称为链式队列。两者的区别仅是顺序表和链表的区别，即在实际的物理空间中，数据集中存储的队列是顺序队列，分散存储的队列是链队列。</p>
<p>队列可以是单端的，也可以是双端的，但是定要遵循的是先进先出的规则，从一端进，从另一端出。</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV03-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/LV03-09-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84-%E9%98%9F%E5%88%97/img/image-20220501085957416.png" alt="image-20220501085957416" style="zoom:33%;" />

<h1 id="二、顺序队列"><a href="#二、顺序队列" class="headerlink" title="二、顺序队列"></a><font size=3>二、顺序队列</font></h1><h2 id="1-存储结构"><a href="#1-存储结构" class="headerlink" title="1.存储结构"></a><font size=3>1.存储结构</font></h2><p>顺序队列是一种顺序存储结构，它借助顺序表实现，由于我们需要在顺序表的一端实现入队，在另一端实现出队，所以需要两个”指针”（为什么加引号呢？严格来说这两个并非是指针变量，而只是代表了顺序队列中指向队头元素和队尾元素所在数组位置下标的变量，一般是整型数据），来指向队头和队尾。</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV03-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/LV03-09-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84-%E9%98%9F%E5%88%97/img/image-20220501091028672.png" alt="image-20220501091028672" style="zoom:33%;" />

<p>按照预想的情况应该是上图的这种结构， front 指向队头元素， rear 指向队尾元素的下一个位置（原因？只有指向下一个位置才能插入嘛，不然这个数据不就被覆盖了嘛，哈哈），接下来我们想象一下从数据入队到全部出队，会发生什么现象。假设有一个队列最大可以有 5 个元素，那么我们让 5 个数据入队然后再出队，然后来看一下最终的状态。</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV03-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/LV03-09-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84-%E9%98%9F%E5%88%97/img/1.gif" alt="1" style="zoom: 50%;" />

<p>我们会发现入队的时候尾指针不断向后移动，出队的时候头指针也是不断向后移动，当<strong>头尾指针重合时，队列中没有元素</strong>，为空队。那么问题产生了，这个时候这个队列是空的，但是队头和队尾已经移动到最后边去了，前边即便空间已经空出来了，由于队列的规则限制，这意味着前边的内存空间是无法使用了，重新申请一个内存空间嘛？显然不太可能喽。那有什么办法吗？可以这样想，我们让队头和队尾指针再重头开始不就行了吗？方案是可行的，那怎么实现呢？</p>
<p>现在要解决的问题就是如何让 front 和 rear 一直自加还能保证只在这 6 个元素的队列中循环，也就是说，不管这两个指针加了多少次，是最后的取值只能在 0-5 之间，这个时候可以想到，不是有个取余数的操作嘛，对 6 取余的话，最后得到的余数一定是小于 6 的，这样，这个问题就解决啦。那我们看看还会不会有问题。</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV03-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/LV03-09-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84-%E9%98%9F%E5%88%97/img/2.gif" alt="2" style="zoom: 50%;" />

<p>绝了，循环倒是实现了，但是，队满的时候，头尾指针重合了，这不完了。这样的话，我不知道它是空是满，万一这个状态是满的，我又继续入队了，那数据不就没得了？这又怎么办呢？没办法，只好牺牲掉数组的一个数据啦，这样当 rear 到最后一个位置的时候就不能再入队了，因为再入一个数据就已经满队了，再经过规则限定，我们来看看最后的效果吧。</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV03-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/LV03-09-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84-%E9%98%9F%E5%88%97/img/3.gif" alt="3" style="zoom: 50%;" />

<p><strong>【总结】</strong></p>
<p>（1） front 指向队头元素的位置;  rear 指向队尾元素的下一个位置。</p>
<p>（2）在队列操作过程中，为了提高效率，以调整指针代替队列元素的移动，并将数组作为循环队列的操作空间。</p>
<p>（3）为区别空队和满队，满队元素个数比数组元素个数少一个。</p>
<h2 id="2-结构体实现"><a href="#2-结构体实现" class="headerlink" title="2.结构体实现"></a><font size=3>2.结构体实现</font></h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 宏定义 */</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> N 6   <span class="comment">/* 定义队列的容量 */</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* 自定义数据类型 */</span></span><br><span class="line"><span class="keyword">typedef</span> <span class="type">int</span> <span class="type">data_t</span>; <span class="comment">/* 定义栈中数据元素的数据类型 */</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">typedef</span> <span class="class"><span class="keyword">struct</span> </span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">	<span class="type">data_t</span> data[N];  <span class="comment">/* 用数组作为队列的储存空间 */</span></span><br><span class="line">	<span class="type">int</span> front;       <span class="comment">/* 指示队头位置的指针 */</span></span><br><span class="line">	<span class="type">int</span> rear;        <span class="comment">/* 指示队尾位置的指针 */</span></span><br><span class="line">&#125;seqqueue;           <span class="comment">/* 顺序队列类型定义 */</span></span><br></pre></td></tr></table></figure>

<p>【说明】顺序队列应为为 sequence queue ，所以后续使用 seqqueue 作为顺序队列结构体以及函数命名的关键字。</p>
<h2 id="3-顺序队列基本操作"><a href="#3-顺序队列基本操作" class="headerlink" title="3.顺序队列基本操作"></a><font size=3>3.顺序队列基本操作</font></h2><p>相关源代码可以在这里下载：</p>
<table>
    <tr>
        <td align="center" width=150>数据结构</td>
        <td align="left"><a href="https://wwz.lanzouy.com/b036yootg" target="_blank">队列  <i class="fa fa-external-link-alt"></i> </a>  (密码:6sez)</td>  
    </tr>
</table>


<h3 id="3-1创建"><a href="#3-1创建" class="headerlink" title="3.1创建"></a><font size=3>3.1创建</font></h3><details class="folding-tag" blue><summary> 点击查看函数实现 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Function: seqqueue_create</span></span><br><span class="line"><span class="comment"> * @Description: 顺序队列创建</span></span><br><span class="line"><span class="comment"> * @param   : none</span></span><br><span class="line"><span class="comment"> * @return  : 返回一个地址</span></span><br><span class="line"><span class="comment"> *            NULL,创建失败;</span></span><br><span class="line"><span class="comment"> *            other,创建成功</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">seqqueue * <span class="title function_">seqqueue_create</span><span class="params">()</span> </span><br><span class="line">&#123;</span><br><span class="line">	<span class="comment">/* 创建一个顺序队列结构体指针变量 */</span></span><br><span class="line">    seqqueue *sq;</span><br><span class="line">	<span class="comment">/* 1. 申请内存空间 */</span></span><br><span class="line">	sq = (seqqueue *)<span class="built_in">malloc</span>(<span class="keyword">sizeof</span>(seqqueue));</span><br><span class="line">	<span class="comment">/* 2. 判断是否申请成功 */</span></span><br><span class="line">	<span class="keyword">if</span>(sq == <span class="literal">NULL</span>)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;sequence queue malloc failed!\n&quot;</span>);</span><br><span class="line">		<span class="keyword">return</span> <span class="literal">NULL</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">/* 3. 初始化申请的内存空间 */</span></span><br><span class="line">	<span class="built_in">memset</span>(sq-&gt;data, <span class="number">0</span>, <span class="keyword">sizeof</span>(sq-&gt;data));</span><br><span class="line">	sq-&gt;front = <span class="number">0</span>;</span><br><span class="line">	sq-&gt;rear = <span class="number">0</span>;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">return</span> sq;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h3 id="3-2释放"><a href="#3-2释放" class="headerlink" title="3.2释放"></a><font size=3>3.2释放</font></h3><details class="folding-tag" blue><summary> 点击查看函数实现 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Function: seqqueue_free</span></span><br><span class="line"><span class="comment"> * @Description: 顺序队列释放</span></span><br><span class="line"><span class="comment"> * @param sq: 顺序队列地址</span></span><br><span class="line"><span class="comment"> * @return  : 返回一个NULL</span></span><br><span class="line"><span class="comment"> *            NULL,释放完毕</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">seqqueue * <span class="title function_">seqqueue_free</span><span class="params">(seqqueue *sq)</span> </span><br><span class="line">&#123;</span><br><span class="line">	<span class="comment">/* 1. 判断顺序队列是否存在 */</span></span><br><span class="line">	<span class="keyword">if</span>(sq == <span class="literal">NULL</span>)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;sequence queue is not existed!\n&quot;</span>);</span><br><span class="line">		<span class="keyword">return</span> <span class="literal">NULL</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">/* 2. 释放结构体指针变量指向的空间 */</span></span><br><span class="line">	<span class="built_in">free</span>(sq);</span><br><span class="line">	sq = <span class="literal">NULL</span>;</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;sequence queue free completed!\n&quot;</span>);</span><br><span class="line">	<span class="keyword">return</span> <span class="literal">NULL</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h3 id="3-3显示"><a href="#3-3显示" class="headerlink" title="3.3显示"></a><font size=3>3.3显示</font></h3><details class="folding-tag" blue><summary> 点击查看函数实现 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Function: seqqueue_show</span></span><br><span class="line"><span class="comment"> * @Description: 顺序队列显示</span></span><br><span class="line"><span class="comment"> * @param sq: 顺序队列地址</span></span><br><span class="line"><span class="comment"> * @return  : 返回一个数值</span></span><br><span class="line"><span class="comment"> *            0,显示完毕;</span></span><br><span class="line"><span class="comment"> *            -1,顺序队列不存在或者队列为空</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">seqqueue_show</span><span class="params">(seqqueue *sq)</span> </span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">int</span> i = <span class="number">0</span>;</span><br><span class="line">	<span class="comment">/* 1. 判断顺序队列是否存在 */</span></span><br><span class="line">	<span class="keyword">if</span>(sq == <span class="literal">NULL</span>)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;sequence queue is not existed!\n&quot;</span>);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">/* 2. 判断顺序队列是否为空 */</span></span><br><span class="line">	<span class="keyword">if</span>(sq-&gt;front == sq-&gt;rear)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;sequence queue is empty!\n&quot;</span>);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">/* 3. 访问顺序队列中所有数据 */</span></span><br><span class="line">	<span class="keyword">if</span>(sq-&gt;front &lt; sq-&gt;rear)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;sequence queue:&quot;</span>);</span><br><span class="line">		<span class="keyword">for</span>(i = sq-&gt;front; i &lt; sq-&gt;rear; i++)</span><br><span class="line">		&#123;</span><br><span class="line">			<span class="built_in">printf</span>(<span class="string">&quot;%d &quot;</span>, sq-&gt;data[i]);</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="built_in">puts</span>(<span class="string">&quot;&quot;</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">else</span></span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;sequence queue:&quot;</span>);</span><br><span class="line">		<span class="keyword">for</span>(i = <span class="number">0</span>; i &lt; N; i++)</span><br><span class="line">		&#123;</span><br><span class="line">			<span class="keyword">if</span>(i == sq-&gt;rear)</span><br><span class="line">				<span class="built_in">printf</span>(<span class="string">&quot;null &quot;</span>);</span><br><span class="line">			<span class="keyword">else</span></span><br><span class="line">				<span class="built_in">printf</span>(<span class="string">&quot;%d &quot;</span>, sq-&gt;data[i]);</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="built_in">puts</span>(<span class="string">&quot;&quot;</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h3 id="3-4入队"><a href="#3-4入队" class="headerlink" title="3.4入队"></a><font size=3>3.4入队</font></h3><details class="folding-tag" blue><summary> 点击查看函数实现 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Function: seqqueue_enqueue</span></span><br><span class="line"><span class="comment"> * @Description: 入队</span></span><br><span class="line"><span class="comment"> * @param sq: 顺序队列地址</span></span><br><span class="line"><span class="comment"> * @param value: 入队数据</span></span><br><span class="line"><span class="comment"> * @return  : 返回一个数值</span></span><br><span class="line"><span class="comment"> *            0,入队成功;</span></span><br><span class="line"><span class="comment"> *            -1,入队失败</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">seqqueue_enqueue</span><span class="params">(seqqueue *sq, <span class="type">data_t</span> value)</span> </span><br><span class="line">&#123;</span><br><span class="line">	<span class="comment">/* 1. 判断顺序队列是否存在 */</span></span><br><span class="line">	<span class="keyword">if</span>(sq == <span class="literal">NULL</span>)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;sequence queue is not existed!\n&quot;</span>);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">/* 2. 判断顺序队列是否已满 */</span></span><br><span class="line">	<span class="keyword">if</span>((sq-&gt;rear + <span class="number">1</span>) % N == sq-&gt;front)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;sequence queue is full!\n&quot;</span>);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">/* 3. 元素入队 */</span></span><br><span class="line">	sq-&gt;data[sq-&gt;rear] = value;</span><br><span class="line">	sq-&gt;rear = (sq-&gt;rear + <span class="number">1</span>) % N;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h3 id="3-5出队"><a href="#3-5出队" class="headerlink" title="3.5出队"></a><font size=3>3.5出队</font></h3><p>【注意】这里出队的时候不要做队列是否存在以及是否为空的判断，这两个判断若是有返回值的话程序会认为返回值就是出队的元素，所以队列的存在性以及是否为空，要在使用出队函数前进行判断。</p>
<details class="folding-tag" blue><summary> 点击查看函数实现 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">**</span><br><span class="line"> * @Function: seqqueue_dequeue</span><br><span class="line"> * @Description: 出队</span><br><span class="line"> * @param sq: 顺序队列地址</span><br><span class="line"> * @<span class="keyword">return</span>  : 返回一个数据</span><br><span class="line"> *            ret,出队数据</span><br><span class="line"> */</span><br><span class="line"><span class="type">data_t</span> seqqueue_dequeue(seqqueue *sq) </span><br><span class="line">&#123;</span><br><span class="line">	<span class="comment">/* 定义一个暂存队列元素的变量 */</span></span><br><span class="line">	<span class="type">data_t</span> ret;</span><br><span class="line">	<span class="comment">/* 1. 判断顺序队列是否存在 */</span></span><br><span class="line">	<span class="comment">/* 需要在函数外进行，这是因为该函数需要返回队列数据，队列数据也可能为这里的返回值 */</span></span><br><span class="line">	<span class="comment">/* 2. 判断顺序队列是否为空 */</span></span><br><span class="line">	<span class="comment">/* 需要在函数外进行，这是因为该函数需要返回队列数据，队列数据也可能为这里的返回值 */</span></span><br><span class="line">	<span class="comment">/* 3. 元素出队*/</span></span><br><span class="line">	ret = sq-&gt;data[sq-&gt;front];</span><br><span class="line">	<span class="comment">/* 4. 更新队头指针 */</span></span><br><span class="line">	sq-&gt;front = (sq-&gt;front + <span class="number">1</span>) % N;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">return</span> ret;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h3 id="3-6空满"><a href="#3-6空满" class="headerlink" title="3.6空满"></a><font size=3>3.6空满</font></h3><details class="folding-tag" blue><summary> 点击查看判断是否为空函数实现 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Function: seqqueue_empty</span></span><br><span class="line"><span class="comment"> * @Description: 顺序队列是否为空</span></span><br><span class="line"><span class="comment"> * @param sq: 顺序队列地址</span></span><br><span class="line"><span class="comment"> * @return  : 返回一个数值</span></span><br><span class="line"><span class="comment"> *            0,顺序队列非空;</span></span><br><span class="line"><span class="comment"> *            1,顺序队列为空;</span></span><br><span class="line"><span class="comment"> *            -1,顺序队列不存在</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">seqqueue_empty</span><span class="params">(seqqueue *sq)</span> </span><br><span class="line">&#123;</span><br><span class="line">	<span class="comment">/* 1. 判断顺序队列是否存在 */</span></span><br><span class="line">	<span class="keyword">if</span>(sq == <span class="literal">NULL</span>)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;sequence queue is not existed!\n&quot;</span>);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">/* 2. 判断队头队尾指针是否重合 */</span></span><br><span class="line">	<span class="keyword">if</span>(sq-&gt;front == sq-&gt;rear)</span><br><span class="line">		<span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">	<span class="keyword">else</span></span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	<span class="comment">/* 或者可以写成下边的形式 */</span></span><br><span class="line">	<span class="comment">// return (sq-&gt;front == sq-&gt;rear ? 1 : 0);</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看判断是否为满函数实现 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Function: seqqueue_full</span></span><br><span class="line"><span class="comment"> * @Description: 顺序队列是否已满</span></span><br><span class="line"><span class="comment"> * @param sq: 顺序队列地址</span></span><br><span class="line"><span class="comment"> * @return  : 返回一个数值</span></span><br><span class="line"><span class="comment"> *            0,顺序队列未满;</span></span><br><span class="line"><span class="comment"> *            1,顺序队列已满;</span></span><br><span class="line"><span class="comment"> *            -1,顺序队列不存在</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">seqqueue_full</span><span class="params">(seqqueue *sq)</span> </span><br><span class="line">&#123;</span><br><span class="line">	<span class="comment">/* 1. 判断顺序队列是否存在 */</span></span><br><span class="line">	<span class="keyword">if</span>(sq == <span class="literal">NULL</span>)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;sequence queue is not existed!\n&quot;</span>);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">/* 2. 判断是否已满 */</span></span><br><span class="line">	<span class="keyword">if</span>((sq-&gt;rear + <span class="number">1</span>) % N == sq-&gt;front)</span><br><span class="line">		<span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">	<span class="keyword">else</span></span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h3 id="3-7清空"><a href="#3-7清空" class="headerlink" title="3.7清空"></a><font size=3>3.7清空</font></h3><details class="folding-tag" blue><summary> 点击查看函数实现 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Function: seqqueue_clear</span></span><br><span class="line"><span class="comment"> * @Description: 顺序队列清空</span></span><br><span class="line"><span class="comment"> * @param sq: 顺序队列地址</span></span><br><span class="line"><span class="comment"> * @return  : 返回一个数值</span></span><br><span class="line"><span class="comment"> *            0,顺序队列已清空;</span></span><br><span class="line"><span class="comment"> *            -1,顺序队列不存在</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">seqqueue_clear</span><span class="params">(seqqueue *sq)</span> </span><br><span class="line">&#123;</span><br><span class="line">	<span class="comment">/* 1. 判断顺序队列是否存在 */</span></span><br><span class="line">	<span class="keyword">if</span>(sq == <span class="literal">NULL</span>)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;sequence queue is not existed!\n&quot;</span>);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">/* 2. 清空顺序队列 */</span></span><br><span class="line">	sq-&gt;rear = sq-&gt;front = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h1 id="三、链式队列"><a href="#三、链式队列" class="headerlink" title="三、链式队列"></a><font size=3>三、链式队列</font></h1><h2 id="1-存储结构-1"><a href="#1-存储结构-1" class="headerlink" title="1.存储结构"></a><font size=3>1.存储结构</font></h2><p>链式队列的存储结构如下图所示，将从队尾入队，队头出队：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV03-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/LV03-09-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84-%E9%98%9F%E5%88%97/img/image-20220501161801389.png" alt="image-20220501161801389" style="zoom:33%;" />

<p>相对于顺序队列，链式队列没有假溢出，因为它不会满，链式队列入队操作会申请内存新建节点，出队会释放相应节点，动态存储，只要能成功申请内存创建新的节点，链式队列就不会满，所以不涉及溢出和队满的情况。（当然了，内存不够的时候也还是会满的啦，不过这个时候就可能需要考虑换更大空间的内存啦，哈哈哈。）</p>
<h2 id="2-结构体实现-1"><a href="#2-结构体实现-1" class="headerlink" title="2.结构体实现"></a><font size=3>2.结构体实现</font></h2><p>对于链式队列，还是借助单链表来实现，然后我们再添加头尾指针指向头尾就好啦。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 自定义数据类型 */</span></span><br><span class="line"><span class="keyword">typedef</span> <span class="type">int</span> <span class="type">data_t</span>; <span class="comment">/* 定义栈中数据元素的数据类型 */</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">typedef</span> <span class="class"><span class="keyword">struct</span> <span class="title">slqueue_node</span> </span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">	<span class="type">data_t</span> data;</span><br><span class="line">	<span class="class"><span class="keyword">struct</span> <span class="title">slqueue_node</span> *<span class="title">next</span>;</span></span><br><span class="line">&#125;slqueuenode, * slqueuelink;</span><br><span class="line"></span><br><span class="line"><span class="keyword">typedef</span> <span class="class"><span class="keyword">struct</span> </span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">	slqueuelink front;</span><br><span class="line">	slqueuelink rear;</span><br><span class="line">&#125;slqueue_sq;</span><br></pre></td></tr></table></figure>

<p>【说明】单链表实现的队列，英文名字是 single linked list queue ，所以我后边都用 slqueue 作为链式队列结构体及函数命名的关键词啦。</p>
<h2 id="3-链式队列基本操作"><a href="#3-链式队列基本操作" class="headerlink" title="3.链式队列基本操作"></a><font size=3>3.链式队列基本操作</font></h2><p>相关源代码可以在这里下载：</p>
<table>
    <tr>
        <td align="center" width=150>数据结构</td>
        <td align="left"><a href="https://wwz.lanzouy.com/b036yootg" target="_blank">队列  <i class="fa fa-external-link-alt"></i> </a>  (密码:6sez)</td>  
    </tr>
</table>


<h3 id="3-1创建-1"><a href="#3-1创建-1" class="headerlink" title="3.1创建"></a><font size=3>3.1创建</font></h3><ul>
<li>（1）链式队列结构体变量  lq  申请内存(包含两个成员： front 和 rear )；</li>
<li>（2）申请一个单链表结构体变量  Head  申请内存空间，也就是头节点的内存空间；</li>
<li>（3）队列结构体指针变量成员的首尾指针均指向  Head ；</li>
<li>（4）初始化单链表结构体变量成员</li>
</ul>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV03-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/LV03-09-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84-%E9%98%9F%E5%88%97/img/image-20220501175330538.png" alt="image-20220501175330538" style="zoom:33%;" />

<p>【注意】（2）、（3）两步可合并，省去 Head  变量的定义即有：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">lq-&gt;front = lq-&gt;rear = (slqueuelink)<span class="built_in">malloc</span>(<span class="keyword">sizeof</span>(slqueuenode));</span><br></pre></td></tr></table></figure>

<details class="folding-tag" blue><summary> 点击查看函数实现 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Function: slqueue_create</span></span><br><span class="line"><span class="comment"> * @Description: 链式队列创建</span></span><br><span class="line"><span class="comment"> * @param   : none</span></span><br><span class="line"><span class="comment"> * @return  : 返回一个地址</span></span><br><span class="line"><span class="comment"> *            NULL,创建失败;</span></span><br><span class="line"><span class="comment"> *            other,创建成功</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">slqueue_lq * <span class="title function_">slqueue_create</span><span class="params">()</span> </span><br><span class="line">&#123;</span><br><span class="line">	<span class="comment">/* 定义一个链式队列的结构体指针变量 */</span></span><br><span class="line">	slqueue_lq *lq;</span><br><span class="line">	<span class="comment">/* 1. 申请链式队列结构体变量的内存空间(头尾指针内存空间) */</span></span><br><span class="line">	lq = (slqueue_lq *)<span class="built_in">malloc</span>(<span class="keyword">sizeof</span>(slqueue_lq));</span><br><span class="line">	<span class="comment">/* 2. 判断链式队列结构体指针变量内存是否申请成功 */</span></span><br><span class="line">	<span class="keyword">if</span>(lq == <span class="literal">NULL</span>)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;single linked list queue malloc failed!\n&quot;</span>);</span><br><span class="line">		<span class="keyword">return</span> <span class="literal">NULL</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">/* 3. 申请一个节点的内存空间(相当于申请一个链表头节点) */</span></span><br><span class="line">	lq-&gt;front = lq-&gt;rear = (slqueuelink)<span class="built_in">malloc</span>(<span class="keyword">sizeof</span>(slqueuenode));</span><br><span class="line">	<span class="comment">/* 4. 判断节点是否申请成功 */</span></span><br><span class="line">	<span class="keyword">if</span>(lq-&gt;front == <span class="literal">NULL</span>)<span class="comment">/* front和rear开始都指向头节点,用谁都可以 */</span></span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;single linked list queue node malloc failed!\n&quot;</span>);</span><br><span class="line">		<span class="built_in">free</span>(lq);<span class="comment">/* 释放掉之前申请的链式队列结构体指针变量内存空间 */</span></span><br><span class="line">		<span class="keyword">return</span> <span class="literal">NULL</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">/* 5. 初始化头节点 */</span></span><br><span class="line">	lq-&gt;front-&gt;data = <span class="number">0</span>;</span><br><span class="line">	lq-&gt;front-&gt;next = <span class="literal">NULL</span>;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">return</span> lq;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h3 id="3-2入队"><a href="#3-2入队" class="headerlink" title="3.2入队"></a><font size=3>3.2入队</font></h3><p>我们从链表尾进行入队操作：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV03-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/LV03-09-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84-%E9%98%9F%E5%88%97/img/4.gif" alt="4" style="zoom:50%;" />

<details class="folding-tag" blue><summary> 点击查看函数实现 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Function: slqueue_enqueue</span></span><br><span class="line"><span class="comment"> * @Description: 链式队列入队</span></span><br><span class="line"><span class="comment"> * @param lq: 链式队列地址</span></span><br><span class="line"><span class="comment"> * @param value: 要入队的数据</span></span><br><span class="line"><span class="comment"> * @return  : 返回一个数值</span></span><br><span class="line"><span class="comment"> *            0,入队成功;</span></span><br><span class="line"><span class="comment"> *            -1,链式队列不存在或者新建节点失败</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">slqueue_enqueue</span><span class="params">(slqueue_lq *lq, <span class="type">data_t</span> value)</span> </span><br><span class="line">&#123;</span><br><span class="line">	<span class="comment">/* 定义一个链式队列节点的结构体指针变量 */</span></span><br><span class="line">	slqueuelink p;</span><br><span class="line">	<span class="comment">/* 1. 判断链式队列是否存在 */</span></span><br><span class="line">	<span class="keyword">if</span>(lq == <span class="literal">NULL</span>)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;single linked list queue is not existed!\n&quot;</span>);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">/* 2. 申请入队节点内存空间 */</span></span><br><span class="line">	p = (slqueuelink)<span class="built_in">malloc</span>(<span class="keyword">sizeof</span>(slqueuenode));</span><br><span class="line">	<span class="comment">/* 3. 判断节点是否申请成功 */</span></span><br><span class="line">	<span class="keyword">if</span>(p == <span class="literal">NULL</span>)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;single linked list queue node malloc failed!\n&quot;</span>);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">/* 4. 初始化新申请的节点 */</span></span><br><span class="line">	p-&gt;data = value;</span><br><span class="line">	p-&gt;next = <span class="literal">NULL</span>;</span><br><span class="line">	<span class="comment">/* 5. 尾部插入节点实现入队 */</span></span><br><span class="line">	lq-&gt;rear-&gt;next = p;</span><br><span class="line">	lq-&gt;rear = p;<span class="comment">/* 尾节点指针移动，p 成为新的尾节点 */</span></span><br><span class="line">	</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h3 id="3-3出队"><a href="#3-3出队" class="headerlink" title="3.3出队"></a><font size=3>3.3出队</font></h3><p>我们将从链表头位置出队，由于我们创建的链表是有头节点的，头节点仅仅是代表这个队列的地址，所以每一次出队我们表面上是将数据出队，实际上这个数据还存在于出队后的头节点中，释放的空间也是出队前头节点的空间：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV03-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/LV03-09-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84-%E9%98%9F%E5%88%97/img/5.gif" alt="5" style="zoom:50%;" />

<p>【注意】这里出队的时候不要做队列是否存在以及是否为空的判断，这两个判断若是有返回值的话程序会认为返回值就是出队的元素，所以队列的存在性以及是否为空，要在使用出队函数前进行判断。</p>
<details class="folding-tag" blue><summary> 点击查看函数实现 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Function: slqueue_dequeue</span></span><br><span class="line"><span class="comment"> * @Description: 链式队列出队</span></span><br><span class="line"><span class="comment"> * @param lq: 链式队列地址</span></span><br><span class="line"><span class="comment"> * @return  : 返回一个出队的元素</span></span><br><span class="line"><span class="comment"> *            0,入队成功;</span></span><br><span class="line"><span class="comment"> *            -1,链式队列不存在或者新建节点失败</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">data_t</span> <span class="title function_">slqueue_dequeue</span><span class="params">(slqueue_lq *lq)</span> </span><br><span class="line">&#123;</span><br><span class="line">	<span class="comment">/* 定义一个链式队列节点的结构体指针变量 */</span></span><br><span class="line">	slqueuelink p;</span><br><span class="line">	<span class="comment">/* 1. 判断链式队列是否存在 */</span></span><br><span class="line">	<span class="comment">/* 此步骤需要在函数外判断，因为这里返回的话，会认为返回值是出队的数据 */</span></span><br><span class="line">	<span class="comment">/* 2. 判断链式队列是否为空 */</span></span><br><span class="line">	<span class="comment">/* 此步骤需要在函数外判断，因为这里返回的话，会认为返回值是出队的数据 */</span></span><br><span class="line">	<span class="comment">/* 3. 出队 */</span></span><br><span class="line">	p = lq-&gt;front;</span><br><span class="line">	lq-&gt;front = p-&gt;next;<span class="comment">/* 下一个节点将成为头指针 */</span></span><br><span class="line">	<span class="comment">/* 3. 释放出队的节点的上一个节点空间 */</span></span><br><span class="line">	<span class="built_in">free</span>(p);</span><br><span class="line">	p = <span class="literal">NULL</span>;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">return</span> (lq-&gt;front-&gt;data);</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h3 id="3-4释放"><a href="#3-4释放" class="headerlink" title="3.4释放"></a><font size=3>3.4释放</font></h3><p>释放的方式与出队一样，只是需要将所有节点出队，并且释放掉最后一个头节点，如下图所示：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV03-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/LV03-09-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84-%E9%98%9F%E5%88%97/img/6.gif" alt="6" style="zoom:50%;" />

<details class="folding-tag" blue><summary> 点击查看函数实现 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Function: slqueue_free</span></span><br><span class="line"><span class="comment"> * @Description: 链式队列释放</span></span><br><span class="line"><span class="comment"> * @param lq: 链式队列地址</span></span><br><span class="line"><span class="comment"> * @return  : 返回一个NULL</span></span><br><span class="line"><span class="comment"> *            NULL,释放完成</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">slqueue_lq * <span class="title function_">slqueue_free</span><span class="params">(slqueue_lq *lq)</span> </span><br><span class="line">&#123;</span><br><span class="line">	<span class="comment">/* 定义一个链式队列节点的结构体指针变量 */</span></span><br><span class="line">	slqueuelink p;</span><br><span class="line">	<span class="comment">/* 1. 判断链式队列是否存在 */</span></span><br><span class="line">	<span class="keyword">if</span>(lq == <span class="literal">NULL</span>)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;single linked list queue is not existed!\n&quot;</span>);</span><br><span class="line">		<span class="keyword">return</span> <span class="literal">NULL</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">/* 2. 释放所有节点空间 */</span></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;single linked list queue free:&quot;</span>);</span><br><span class="line">	<span class="keyword">while</span>(lq-&gt;front)</span><br><span class="line">	&#123;</span><br><span class="line">		p = lq-&gt;front;</span><br><span class="line">		lq-&gt;front = p-&gt;next;<span class="comment">/* 下一个节点成为新的头节点 */</span></span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;%d &quot;</span>, p-&gt;data);</span><br><span class="line">		<span class="built_in">free</span>(p);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="built_in">puts</span>(<span class="string">&quot;&quot;</span>);</span><br><span class="line">	<span class="comment">/* 3. 释放链式队列的内存空间(front和rear指针) */</span></span><br><span class="line">	<span class="built_in">free</span>(lq);</span><br><span class="line">	lq = <span class="literal">NULL</span>;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">return</span> <span class="literal">NULL</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h3 id="3-5清空"><a href="#3-5清空" class="headerlink" title="3.5清空"></a><font size=3>3.5清空</font></h3><p>清空的过程，也是将所有节点出队，只不过不释放最后一个节点：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV03-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/LV03-09-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84-%E9%98%9F%E5%88%97/img/7.gif" alt="7" style="zoom:50%;" />

<details class="folding-tag" blue><summary> 点击查看函数实现 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Function: slqueue_clear</span></span><br><span class="line"><span class="comment"> * @Description: 链式队列清空</span></span><br><span class="line"><span class="comment"> * @param lq: 链式队列地址</span></span><br><span class="line"><span class="comment"> * @return  : 返回一个数值</span></span><br><span class="line"><span class="comment"> *            0,清空完成;</span></span><br><span class="line"><span class="comment"> *            -1,链式队列不存在</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">slqueue_clear</span><span class="params">(slqueue_lq *lq)</span> </span><br><span class="line">&#123;</span><br><span class="line">	<span class="comment">/* 定义一个链式队列节点的结构体指针变量 */</span></span><br><span class="line">	slqueuelink p;</span><br><span class="line">	<span class="comment">/* 1. 判断链式队列是否存在 */</span></span><br><span class="line">	<span class="keyword">if</span>(lq == <span class="literal">NULL</span>)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;single linked list queue is not existed!\n&quot;</span>);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">/* 2. 清空链式队列 */</span></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;single linked list queue clear free:&quot;</span>);</span><br><span class="line">	<span class="keyword">while</span>(lq-&gt;front-&gt;next != <span class="literal">NULL</span>)<span class="comment">/* 结束条件是剩余一个节点 */</span></span><br><span class="line">	&#123;</span><br><span class="line">		p = lq-&gt;front;</span><br><span class="line">		lq -&gt;front = p-&gt;next;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;%d &quot;</span>, p-&gt;data);</span><br><span class="line">		<span class="built_in">free</span>(p);</span><br><span class="line">		p = <span class="literal">NULL</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="built_in">puts</span>(<span class="string">&quot;&quot;</span>);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h3 id="3-6显示"><a href="#3-6显示" class="headerlink" title="3.6显示"></a><font size=3>3.6显示</font></h3><p>此函数将显示所有节点的数据，包括头节点。</p>
<details class="folding-tag" blue><summary> 点击查看函数实现 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Function: slqueue_show</span></span><br><span class="line"><span class="comment"> * @Description: 链式队列所有节点显示(包括头节点)</span></span><br><span class="line"><span class="comment"> * @param lq: 链式队列地址</span></span><br><span class="line"><span class="comment"> * @return  : 返回一个数值</span></span><br><span class="line"><span class="comment"> *            0,显示完成;</span></span><br><span class="line"><span class="comment"> *            -1,链式队列不存在</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">slqueue_show</span><span class="params">(slqueue_lq *lq)</span> </span><br><span class="line">&#123;</span><br><span class="line">	<span class="comment">/* 定义一个链式队列节点的结构体指针变量 */</span></span><br><span class="line">	slqueuelink p;</span><br><span class="line">	<span class="comment">/* 1. 判断链式队列是否存在 */</span></span><br><span class="line">	<span class="keyword">if</span>(lq == <span class="literal">NULL</span>)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;single linked list queue is not existed!\n&quot;</span>);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">/* 2. 遍历链式队列 */</span></span><br><span class="line">	p = lq-&gt;front;</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;single linked list queue:&quot;</span>);</span><br><span class="line">	<span class="keyword">while</span>(p != <span class="literal">NULL</span>)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;%d &quot;</span>, p-&gt;data);</span><br><span class="line">		p = p-&gt;next;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="built_in">puts</span>(<span class="string">&quot;&quot;</span>);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h3 id="3-7判空"><a href="#3-7判空" class="headerlink" title="3.7判空"></a><font size=3>3.7判空</font></h3><p>此函数将显示所有节点的数据，包括头节点。</p>
<details class="folding-tag" blue><summary> 点击查看函数实现 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Function: slqueue_empty</span></span><br><span class="line"><span class="comment"> * @Description: 链式队列是否为空</span></span><br><span class="line"><span class="comment"> * @param lq: 链式队列地址</span></span><br><span class="line"><span class="comment"> * @return  : 返回一个数值</span></span><br><span class="line"><span class="comment"> *            0,链式队列非空;</span></span><br><span class="line"><span class="comment"> *            1,链式队列为空;</span></span><br><span class="line"><span class="comment"> *            -1,链式队列不存在</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">slqueue_empty</span><span class="params">(slqueue_lq *lq)</span> </span><br><span class="line">&#123;</span><br><span class="line">	<span class="comment">/* 1. 判断链式队列是否存在 */</span></span><br><span class="line">	<span class="keyword">if</span>(lq == <span class="literal">NULL</span>)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;single linked list queue is not existed!\n&quot;</span>);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">/* 2. 判断front和rear是否相等 */</span></span><br><span class="line">	<span class="keyword">if</span>(lq-&gt;front == lq-&gt;rear)</span><br><span class="line">		<span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">	<span class="keyword">else</span></span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	<span class="comment">/* 或者可以写成下边的情况 */</span></span><br><span class="line">	<span class="comment">// return (lq-&gt;front == lq-&gt;rear ? 1 : 0);</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
              </div>
            </details>


    </div>

    
    
    

    <footer class="post-footer">




    <div>
        
            <div style="text-align:center;color: #ccc;font-size:14px;">
            ----------本文结束
            <i class="fas fa-fan fa-spin" style="color: #FF1493; font-size: 1rem"></i>
            感谢您的阅读----------
            </div>
        
    </div>





  
  <div class="my_post_copyright"> 
    <p><span>文章标题:</span><a href="/post/ddef650e.html">LV03-09-数据结构-队列</a></p>
    <p><span>文章作者:</span><a href="/" title="欢迎访问 《苏木》 的学习笔记">苏木</a></p>
    <p><span>发布时间:</span>2022年04月25日 - 09:44</p>
    <p><span>最后更新:</span>2025年06月14日 - 00:25</p>
    <p><span>原始链接:</span><a href="/post/ddef650e.html" title="LV03-09-数据结构-队列">https://sumumm.github.io/post/ddef650e.html</a></p>
    <p><span>许可协议:</span><i class="fab fa-creative-commons"></i> <a rel="license" href= "https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank" title="Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)">署名-非商业性使用-禁止演绎 4.0 国际</a> 转载请保留原文链接及作者。</p>  
  </div>
  


          <div class="post-tags">
              <a href="/tags/LV03-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/" rel="tag"><i class="fa fa-tag"></i> LV03-数据结构</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/post/f6b5194b.html" rel="prev" title="LV03-10-数据结构-树">
                  <i class="fa fa-angle-left"></i> LV03-10-数据结构-树
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/post/2dee0fea.html" rel="next" title="LV03-08-数据结构-栈">
                  LV03-08-数据结构-栈 <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 2017 – 
    <span itemprop="copyrightYear">2025</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">苏木</span>
  </div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
      <span>站点总字数：</span>
    <span title="站点总字数">3.7m</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span>站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">225:26</span>
  </span>
</div>




    <span id="sitetime"></span>
    <script defer language=javascript>
        function siteTime()
        {
            window.setTimeout("siteTime()", 1000);
            var seconds = 1000;
            var minutes = seconds * 60;
            var hours = minutes * 60;
            var days = hours * 24;
            var years = days * 365;
            var today = new Date();
            var todayYear = today.getFullYear();
            var todayMonth = today.getMonth()+1;
            var todayDate = today.getDate();
            var todayHour = today.getHours();
            var todayMinute = today.getMinutes();
            var todaySecond = today.getSeconds();
            /*==================================================
            Date.UTC() -- 返回date对象距世界标准时间(UTC)1970年1月1日午夜之间的毫秒数(时间戳)
            year        - 作为date对象的年份，为4位年份值
            month       - 0-11之间的整数，做为date对象的月份
            day         - 1-31之间的整数，做为date对象的天数
            hours       - 0(午夜24点)-23之间的整数，做为date对象的小时数
            minutes     - 0-59之间的整数，做为date对象的分钟数
            seconds     - 0-59之间的整数，做为date对象的秒数
            microseconds - 0-999之间的整数，做为date对象的毫秒数
            ==================================================*/
            var t1 = Date.UTC(2017, 
                              5, 
                              19, 
                              0, 
                              0, 
                              0); //北京时间
            var t2 = Date.UTC(todayYear,todayMonth,todayDate,todayHour,todayMinute,todaySecond);
            var diff = t2-t1;
            var diffYears = Math.floor(diff/years);
            var diffDays = Math.floor((diff/days)-diffYears*365);
            var diffHours = Math.floor((diff-(diffYears*365+diffDays)*days)/hours);
            var diffMinutes = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours)/minutes);
            var diffSeconds = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours-diffMinutes*minutes)/seconds);
            document.getElementById("sitetime").innerHTML="已在这里 "+diffYears+" 年 "+diffDays+" 天 "+diffHours+" 小时 "+diffMinutes+" 分钟 "+diffSeconds+" 秒";
        }
        siteTime();
    </script>



    </div>
  </footer>

  
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/next-theme-pjax/0.6.0/pjax.min.js" integrity="sha256-vxLn1tSKWD4dqbMRyv940UYw4sXgMtYcK6reefzZrao=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.umd.js" integrity="sha256-ytMJGN3toR+a84u7g7NuHm91VIR06Q41kMWDr2pq7Zo=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/lozad.js/1.16.0/lozad.min.js" integrity="sha256-mOFREFhqmHeQbXpK2lp4nA3qooVgACfh88fpJftLBbc=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/pjax.js"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.1/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>




  <script src="/js/third-party/fancybox.js"></script>

  <script src="/js/third-party/pace.js"></script>


  




  

  <script class="next-config" data-name="enableMath" type="application/json">false</script><script class="next-config" data-name="mathjax" type="application/json">{"enable":true,"tags":"none","js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.js","integrity":"sha256-MASABpB4tYktI2Oitl4t+78w/lyA+D7b/s9GEP0JOGI="}}</script>
<script src="/js/third-party/math/mathjax.js"></script>


 
        <div id="click-show-text"
            data-mobile = false
            data-text = 富强,民主,文明,和谐,自由,平等,公正,法制,爱国,敬业,诚信,友善
            data-fontsize = 15px
            data-random= false>
        </div>
       

      
        <script async src=https://cdn.jsdelivr.net/npm/hexo-next-mouse-effect@latest/click/showText.js></script>
      

      
    




    <script async src="/js/fancybox_param.js"></script>





<!-- APlayer本体 -->



</body>
</html>
